﻿using mozhi.smarterp.BasicData.Customers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;
using Volo.Abp;
using mozhi.smarterp.BasicData.Suppliers.Repositories;
using mozhi.smarterp.BasicData.Suppliers.Services;

namespace mozhi.smarterp.BasicData.Suppliers
{
    public class SupplierAppService : BasicDataAppService, ISupplierAppService
    {
        private ISupplierRepository _repository;
        private SupplierManager _manager;
        private IGuidGenerator _guidGenerator;

        public SupplierAppService(ISupplierRepository repository, SupplierManager manager, IGuidGenerator guidGenerator)
        {
            _repository = repository;
            _manager = manager;
            _guidGenerator = guidGenerator;
        }

        public async Task<PagedResultDto<SupplierListDto>> GetListAsync(PagedResultRequestDto filter)
        {
            var suppliers = await _repository.GetPagedListAsync(filter.SkipCount, filter.MaxResultCount, nameof(Customer.Code));

            var count = await _repository.GetCountAsync();

            var result = new PagedResultDto<SupplierListDto>()
            {
                Items = ObjectMapper.Map<List<Supplier>, List<SupplierListDto>>(suppliers),
                TotalCount = count
            };

            return result;
        }

        public async Task<SupplierDto> GetAsync(Guid id)
        {
            var supplier = await _repository.GetAsync(id);

            return ObjectMapper.Map<Supplier, SupplierDto>(supplier);
        }

        public async Task<SupplierDto> CreateAsync(SupplierCreateDto input)
        {
            var supplier = await _manager.CreateAsync(_guidGenerator.Create(), input.Code, input.Name, input.Activated);

            supplier.ChangeRegionAndCountry(input.Country, input.Region);
            supplier.ChangeContactAddress(input.MailAddress, input.PostalCode, input.Country);


            input.ContactItems = input.ContactItems ?? new List<SupplierContactItemCreateDto>();

            foreach (var item in input.ContactItems)
            {
                var contactItem = new SupplierContactItem(_guidGenerator.Create(), supplier.Id, item.Name, item.JobTitle, item.Telephone, item.Mobilephone, item.Email);

                supplier.ContactItems.Add(contactItem);
            }

            supplier = await _repository.InsertAsync(supplier);

            return ObjectMapper.Map<Supplier, SupplierDto>(supplier);
        }

        public async Task<SupplierDto> UpdateAsync(SupplierUpdateDto input)
        {
            var supplier = await _manager.UpdateAsync(input.Id, input.Code, input.Name, input.Activated);

            supplier.ChangeRegionAndCountry(input.Country, input.Region);

            supplier.ChangeContactAddress(input.MailAddress, input.PostalCode, input.Country);

            input.ContactItems = input.ContactItems ?? new List<SupplierContactItemUpdateDto>();

            var contactItems = new List<SupplierContactItem>();

            foreach (var item in input.ContactItems)
            {
                item.Id = item.Id == default ? _guidGenerator.Create() : item.Id;

                var contactItem = new SupplierContactItem(item.Id, supplier.Id, item.Name, item.JobTitle, item.Telephone, item.Mobilephone, item.Email);
                contactItems.Add(contactItem);
            }

            supplier.ChangeContactItems(contactItems);

            supplier = await _repository.UpdateAsync(supplier);

            return ObjectMapper.Map<Supplier, SupplierDto>(supplier);
        }

        public async Task DeleteAsync(Guid id)
        {
            var customer = await _repository.GetAsync(id);

            if (customer == null)
                throw new BusinessException($"供应商：{id}不存在", "100000");

            await _repository.DeleteAsync(id);
        }

    }
}
